package com.example.smartcity4.login;// 以下省略了 import 语句，请确保已经导入相应的类

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.smartcity4.MainActivity;
import com.example.smartcity4.R;
import com.example.smartcity4.javaBean.GsonBean;
import com.example.smartcity4.javaBean.Logon;

import com.example.smartcity4.reteofi.SmartService;
import com.example.smartcity4.ui.user.LoginActivity;
import com.google.gson.Gson;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LogOnActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView title;
    private ImageView left;
    private ImageView more;
    private EditText item1;
    private EditText item2;
    private EditText item3;
    private RadioButton man;
    private RadioButton woman;
    private RadioGroup radio;
    private LinearLayout login;
    private Button btn;
    private String flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_on);
        initView();
    }

    private void initView() {
        title = findViewById(R.id.title);
        left = findViewById(R.id.left);
        more = findViewById(R.id.more);
        item1 = findViewById(R.id.item1);
        item2 = findViewById(R.id.item2);
        item3 = findViewById(R.id.item3);
        man = findViewById(R.id.man);
        woman = findViewById(R.id.woman);
        radio = findViewById(R.id.radio);
        login = findViewById(R.id.login);
        btn = findViewById(R.id.btn);

        radio.setOnCheckedChangeListener((group, checkedId) -> {
            flag = ((RadioButton) group.findViewById(checkedId)).getText().toString().equals("男") ? "1" : "0";
        });

        btn.setOnClickListener(this);
        login.setOnClickListener(v -> startActivity(new Intent(getApplicationContext(), LoginActivity.class)));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn) {
            submit();
        }
    }

    private void submit() {
        if (validateInput()) {
            // 构建请求体
            Logon bean = new Logon(item1.getText().toString().trim(), item2.getText().toString().trim(),
                    item3.getText().toString().trim(), flag);

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(MainActivity.host)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            SmartService service = retrofit.create(SmartService.class);

            Gson gson = new Gson();
            String obj = gson.toJson(bean);
            RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), obj);

            Call<GsonBean> call = service.post_logon(body);
            call.enqueue(new Callback<GsonBean>() {
                @Override
                public void onResponse(Call<GsonBean> call, Response<GsonBean> response) {
                    if (Integer.valueOf(response.body().code) == 200) {
                        Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(getApplicationContext(), LoginActivity.class));
                    } else {
                        Toast.makeText(getApplicationContext(), "注册失败" + response.body().msg, Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<GsonBean> call, Throwable throwable) {
                    Toast.makeText(getApplicationContext(), "网络加载失败", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    private boolean validateInput() {
        String item1String = item1.getText().toString().trim();
        if (TextUtils.isEmpty(item1String)) {
            Toast.makeText(this, "请输入昵称", Toast.LENGTH_SHORT).show();
            return false;
        }

        String item2String = item2.getText().toString().trim();
        if (TextUtils.isEmpty(item2String)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return false;
        }

        String item3String = item3.getText().toString().trim();
        if (TextUtils.isEmpty(item3String)) {
            Toast.makeText(this, "请输入电话号码", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }
}
